package com.genghis.ptas.question;

import com.genghis.ptas.question.entity.Question;
import com.genghis.ptas.question.service.AnswerService;
import com.genghis.ptas.question.service.QuestionService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.LoginUserUtil;
import com.genghis.ptas.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("question/ask")
public class AskController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;

    @Autowired
    private AnswerService answerService;

    @RequestMapping(value = "addAskDo", method = {RequestMethod.POST})
    @ResponseBody
    public void addAskDo(Question question) {
        questionService.addQuestion(question);
    }

    @RequestMapping(value = "editAskDo", method = {RequestMethod.POST})
    @ResponseBody
    public void editAskDo(Question question) {
        questionService.editQuestion(question);
    }

    @RequestMapping(value = "delAsk", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delAsk(String ids) {
        questionService.delAskByIds(ids);
    }

    @RequestMapping(value = "questionInfoDo", method = {RequestMethod.POST, RequestMethod.GET})
    public String questionInfoDo(HttpServletRequest request, @RequestParam("questionId") Integer questionId) {
        Question question = questionService.findQuestionById(questionId);
        User user = userService.findUserAllInfoById(question.getUserId());
        request.setAttribute("myQuestion", user.getLoginName().equals(LoginUserUtil.findLoginUserName()));
        request.setAttribute("questionInfo", question);
        request.setAttribute("answerList", answerService.findAllAnswerByQuestionId(questionId));
        return "frontstage/question/questionInfoDo";
    }

}
